package edu.mtu.citizenscience.gpsservice;
import edu.mtu.citizenscience.gpsservice.CSGpsService.CSGpsBinder;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;


/*
 * This is a convenience class for binding to the CSGpsService.
 * Activities that wish to make connections to CSLocationListenerService or need to:
 * 
 * 1. Implement CSLocationListenerContianer interface.
 * 2. Have an member references for
 * 
 * 		private CSLocationListener locationListener; // the service
 * 		private CSLocationListenerConnection mConnection; // the connection
 * 
 * 3. Make the location by making the connection, the intent and binding
 * 
 * 		mConnection = new CSLocationListenerConnection(this);
 * 		Intent intent = new Intent(this, CSGpsService.class);
 * 		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
 * 
 * This can be done in either onCreate() or onStart() methods.
 * Note the connection is not made until the activity is running which after onResume(). 
 * So accessing the services in these methods should be guarded with 
 * 
 * 		if(this.mConnection.isBound()) locationListener.setLooseUpdates();
 * 
 * 
 */
public class CSLocationListenerConnection implements ServiceConnection {
	private final static String CLASS_NAME = "CSLocationListenerConnection";
	private static final boolean DEBUG = true;
	private CSLocationListenerContainer parent;
	private boolean mBound = false;
	
	public CSLocationListenerConnection(CSLocationListenerContainer parent)
	{
		this.parent = parent;
	}
	
	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		if (DEBUG) Log.d("DisplayLocation",  "OnServiceConnected");
        
        CSGpsBinder binder = (CSGpsBinder) service;
        parent.setCSLocationListner(binder.getLocationListener());
        mBound = true;
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {mBound = false;}
	
	public boolean isBound(){return mBound;}

}
